###############################################################################
############# Script for replication - MAIN MANUSCRIPT  ####################
###############################################################################
# R version 4.3.1
# (!) Run this script before running script for appendix


######### SET WORKING DIRECTORY TO FOLDER CONTAINING REPLICATION FILES #########
# create working directory
working_directory <- ""
setwd(working_directory)

# load in packages
library(logr)
library(tidyverse)
library(haven)
library(foreign)
library(sjlabelled)
library(labelled)
library(sjPlot)
library(lsr)
library(dplyr)
library(tidyr)
library(forcats)
library(cregg)
library(data.table) 
library(ggplot2)
library(psych) 
library(sjstats)
library(sjmisc)
library(broom)
library(hrbrthemes)
library(egg)
library(grid)
library(kableExtra)
library(modelsummary)
library(expss)
library(nFactors)
library(readr)


######### CONJOINT ANALYSES #########
# use CREGG package for all conjoint analyses (https://cran.r-project.org/web/packages/cregg/cregg.pdf)

##### 1. ALL COUNTRIES #####
## Reproduce: 
##      - Figure 1 (main text)  
##
## Output: 
##      Figure 1: Effects of minipublic attributes on support across countries 
##

# load in data
cj_all <- read_sav ("conjointdata_long_combined.sav") 
#cj_all %>% sjPlot::view_df()

# prepare conjoint attribute variables:
# as factors +
# labeling 
cj_all$issue<-factor(cj_all$issue, labels = c("Emissions", "Immigration"))
cj_all$initiative <-factor(cj_all$initiative, labels = c("Think Tank/NGO", "Government"))
cj_all$recruitment <-factor(cj_all$recruitment, labels = c("Random selection", "Self-selection"))
cj_all$size <-factor(cj_all$size, labels = c("Small", "Large"))
cj_all$composition <-factor(cj_all$composition, labels = c("Citizens alone", "Mixed groups"))
cj_all$majority <-factor(cj_all$majority, labels = c("Narrow majority", "Clear majority"))
cj_all$aim <-factor(cj_all$aim, labels = c("Efficiency", "Consideration of all interests"))
cj_all$outcome.fav <- factor(cj_all$outcome.fav, labels = c("Preference mismatch","Preference match"))
cj_all$authorization <-factor(cj_all$authorization, labels = c("Binding decision",  "Recommendation to public officials", "Recommendation to a public referendum"))
cj_all$authorization <- relevel(cj_all$authorization, ref = "Recommendation to public officials")
cj_all$ID <- as.factor(cj_all$respondent_id)

# rename attributes
feature_names <- c(
  'issue' = "Policy Issue",
  'initiative' = "Initiative",
  'recruitment' = "Recruitment",
  'size' = "Group Size",
  'composition' = "Group Composition",
  'majority' = "Degree of Consensus",
  'aim' = "Aim of the forum",
  'authorization' = "Authorization",
  'outcome.fav' = "Outcome Favorability"
)

# specifying conjoint models
Mchoice <- chosen ~ issue + initiative + recruitment + size + composition + majority + aim + authorization + outcome.fav # conjoint with all attributes, outcome favorability, choice outcome
# estimate conjoint benchmark model
amce_choice <- cregg::cj(cj_all, Mchoice, id = ~ID,  estimate="amce", feature_labels = feature_names) # AMCE benchmark model
	
plot(amce_choice, feature_headers = FALSE)+
  ggplot2::facet_grid(feature~.,  scales = "free_y", space = "free", switch ="y") +
  theme(panel.border = element_rect(linetype = "blank"),
        panel.background = element_blank(), 
        axis.text.y = element_text(colour = "#515151", size = 12),
        axis.text.x = element_text(colour = "#515151", size = 12),
        axis.title = element_text(colour = "#515151", size = 12),
        panel.grid.major = element_line(colour = "grey", linetype = "dotted"),
        axis.ticks = element_blank(), 
        legend.position = "none",
        panel.spacing.y = unit(1, "lines"),
        strip.background= element_rect(color="white", size = 2, fill = "gray97"), 
        strip.text.y.left = element_text(angle = 0),
        strip.placement = "outside",
        strip.text.y = element_text(colour = "#515151", size = 10, face = "bold")) +
  theme(plot.title = element_text(colour =  "#515151", size = 14)) +
  geom_point(size=2) +
  scale_color_manual(values = rep("#515151", 9))

##### 2. DIFFERENCES BETWEEN COUNTRIES  #####
## Reproduce: 
##      - Figure 2 (main text)  
##
## Output: 
##      Figure 2: Differences between countries 
##

# define country variable as factor
cj_all$country <- factor(cj_all$country, labels = c("Finland", "Ireland", "USA"))

# estimate conjoint model (by ~ country)
amce_by <- cj(cj_all, Mchoice, id = ~ID, estimate = "amce", feature_labels = feature_names, feature_order = c("outcome.fav",  "authorization", "aim", "majority", "composition",  "size", "recruitment", "initiative", "issue"), by = ~country)

plot(amce_by, group = "country", feature_headers = FALSE)+
  ggplot2::facet_wrap(~BY, ncol = 3L) +
  theme(panel.border = element_rect(linetype = "blank"),
        panel.background = element_blank(), 
        axis.text.y = element_text(colour = "#515151", size = 12),
        axis.text.x = element_text(colour = "#515151", size = 12),
        axis.title = element_text(colour = "#515151", size = 12),
        panel.grid.major = element_line(colour = "grey", linetype = "dotted"),
        axis.ticks = element_blank(), 
        legend.position = "none",
        panel.spacing.y = unit(1, "lines"),
        strip.background= element_rect(color="white", size = 2, fill = "gray97"), 
        strip.text.y.left = element_text(angle = 0),
        strip.placement = "outside",
        strip.text.y = element_text(colour = "#515151", size = 10, face = "bold")) +
  theme(plot.title = element_text(colour =  "#515151", size = 14)) +
  geom_point(size=2) +
  scale_color_manual(values = c("#515151", "#515151", "#515151"))


##### 3. FAMILIARITY & EXPERIENCES: ALL COUNTRIES #####
# load in country specific data including relevant covariates 
cj_fin <- foreign::read.spss("conjointdata_long_FIN_select.sav", to.data.frame = TRUE, use.value.labels = FALSE)
cj_ire <- foreign::read.spss("conjointdata_long_IRE_select.sav", to.data.frame = TRUE, use.value.labels = FALSE)
cj_usa <- foreign::read.spss("conjointdata_long_USA_select.sav", to.data.frame = TRUE, use.value.labels = FALSE)

# select and rename relevant variables per country dataset
cj_fin <- cj_fin %>% 
   dplyr::rename(
    swd = q2_satisfaction,
    trust_parliament = q3_1,
    trust_government = q3_2,
    trust_politicians = q3_3,
    salience_climate = q12_1,
    salience_immigration = q12_2,
    experiences = q15,
    experiences_direct = q16,
    retro = q20_retro,
    trust_dmp = q21_trust,
    trust_citizens1 = q25_1,
    trust_citizens2 = q25_2
  )

cj_usa <- cj_usa %>%
  dplyr::rename(
    swd = q2_US,
    trust_parliament = q3_01,
    trust_government = q3_02,
    trust_politicians = q3_03,
    salience_climate = q12_01,
    salience_immigration = q12_02,
    experiences = q15,
    experiences_direct = q16,
    retro = q20,
    trust_dmp = q21,
    trust_citizens1 = q22_01,
    trust_citizens2 = q22_02
  )

cj_ire <- cj_ire %>%
  dplyr::rename(
    swd = q2_IRE,
    trust_parliament = q3_01,
    trust_government = q3_02,
    trust_politicians = q3_03,
    salience_climate = q12_01,
    salience_immigration = q12_02,
    experiences = q15,
    experiences_direct = q16,
    retro = q20,
    trust_dmp = q21,
    trust_citizens1 = q22_01,
    trust_citizens2 = q22_02
  )

# define missing values
d_fin <- cj_fin %>%
  select(trust_parliament:trust_citizens2) %>%
  psych::describe(quant=c(.25,.75)) %>%
  as_tibble(rownames="rowname")  %>%
  print()

cj_fin$trust_parliament [cj_fin$trust_parliament==8] <- NA
cj_fin$trust_government [cj_fin$trust_government==8] <- NA
cj_fin$trust_politicians [cj_fin$trust_politicians==8] <- NA

d_ire <- cj_ire %>%
  select(trust_parliament:trust_citizens2) %>%
  psych::describe(quant=c(.25,.75)) %>%
  as_tibble(rownames="rowname")  %>%
  print()

cj_ire$trust_parliament [cj_ire$trust_parliament==997] <- NA
cj_ire$trust_government [cj_ire$trust_government==997] <- NA
cj_ire$trust_politicians [cj_ire$trust_politicians==997] <- NA

d_usa <- cj_usa %>%
  select(trust_parliament:trust_citizens2) %>%
  psych::describe(quant=c(.25,.75)) %>%
  as_tibble(rownames="rowname")  %>%
  print()

cj_usa$trust_parliament [cj_usa$trust_parliament==997] <- NA
cj_usa$trust_government [cj_usa$trust_government==997] <- NA
cj_usa$trust_politicians [cj_usa$trust_politicians==997] <- NA

# merge three country datasets to one dataset
cj_trust <- rbind(cj_fin, cj_ire, cj_usa) # (!) dataset for further analyses

# prepare conjoint attribute variables:
# as factors +
# labeling
cj_trust$issue<-factor(cj_trust$issue, labels = c("Emissions", "Immigration"))
cj_trust$initiative <-factor(cj_trust$initiative, labels = c("Think Tank/NGO", "Government"))
cj_trust$recruitment <-factor(cj_trust$recruitment, labels = c("Random selection", "Self-selection"))
cj_trust$size <-factor(cj_trust$size, labels = c("Small", "Large"))
cj_trust$composition <-factor(cj_trust$composition, labels = c("Citizens alone", "Mixed groups"))
cj_trust$majority <-factor(cj_trust$majority, labels = c("Narrow majority", "Clear majority"))
cj_trust$aim <-factor(cj_trust$aim, labels = c("Efficiency", "Consideration of all interests"))
cj_trust$outcome.fav <- factor(cj_trust$outcome.fav, labels = c("Preference mismatch","Preference match"))
cj_trust$authorization <-factor(cj_trust$authorization, labels = c("Binding decision",  "Recommendation to public officials", "Recommendation to a public referendum"))
cj_trust$authorization <- relevel(cj_trust$authorization, ref = "Recommendation to public officials")
cj_trust$ID <- as.factor(cj_trust$respondent_id)

# rename attributes
feature_names <- c(
  'issue' = "Policy Issue",
  'initiative' = "Initiative",
  'recruitment' = "Recruitment",
  'size' = "Group Size",
  'composition' = "Group Composition",
  'majority' = "Degree of Consensus",
  'aim' = "Aim of the forum",
  'authorization' = "Authorization",
  'outcome.fav' = "Outcome Favorability"
)

# specifying conjoint models
Mchoice <- chosen ~ issue + initiative + recruitment + size + composition + majority + aim + authorization + outcome.fav # conjoint with all attributes, outcome favorability, choice outcome

## Reproduce: 
##      -  Figure 3 (main text) 
##       
## Output: 
##      Figure 3: Effects of minipublic attributes on support conditional on familiarity and experiences 
##      

# copy dataset and use new one
cj_experience <- cj_trust
#cj_experience %>% sjPlot::view_df()

#### a) Dummy (familiar vs. unfamiliar) ####
# 1 - dk, 2 - heard, 3 - know, 4 - participated

# prepare grouping variable for familiarity:
# use distinct measure: at least know well vs. unfamiliar and heard about
cj_experience$familiar <- ifelse(cj_experience$experiences > 2, 1, 0)
cj_experience$familiar <- as.factor(cj_experience$familiar)
summary(cj_experience$familiar)

#### b) Dummy (participated vs. not) ####
# contrast those who have participated (4,4%!) vs. those who have not
cj_experience$participated <- ifelse(cj_experience$experiences == 4, 1, 0)
cj_experience$participated <- as.factor(cj_experience$participated)
summary(cj_experience$participated)

#### c) Direction of experiences (positive vs negative) ####
# prepare variable for experience
describe(cj_experience$experiences_direct)
prop.table(table(cj_experience$experiences_direct))

# select relevant experience variables
cj_expdirect<- cj_experience %>% select(respondent_id:chosen, outcome.fav, experiences, experiences_direct, retro, ID)
# class(cj_expdirect$experiences_direct)
# only select cases that are not 1 (omit respondents who stated to be unfamiliar with minipublics)
prop.table(table(cj_experience$experiences))
cj_expdirect <-  cj_expdirect %>% filter(experiences != 1)

# prepare grouping variable for experience:
# median split  (use dichotomize function)
cj_expdirect$exsplit<-dicho(cj_expdirect$experiences_direct, dich.by = "median", as.num = TRUE)
cj_expdirect$exsplit <- factor(cj_expdirect$exsplit, labels = c("negative experiences", "positive experiences"))
# check whether any of the differences in effects are significant
cj_anova(cj_expdirect, Mchoice, by = ~exsplit)
summary(cj_expdirect$exsplit)

#### d) Combined plots for familiarity and experiences (Figure 3) ####
# specify and estimate three conjoint models for "familiarity" and "participated" and "direction" use difference estimates
diff_amce_exp_choice <- cj(cj_experience, Mchoice, id = ~ID, estimate="amce_diff", feature_labels = feature_names, feature_order = c("outcome.fav", "authorization", "aim", "majority", "composition", "size", "recruitment", "initiative", "issue"), by = ~familiar)
diff_amce_participated_choice <- cj(cj_experience, Mchoice, id = ~ID, estimate="amce_diff", feature_labels = feature_names, feature_order = c("outcome.fav", "authorization", "aim", "majority", "composition", "size", "recruitment", "initiative", "issue"), by = ~participated)
diff_amce_expdirect_choice <- cj(cj_expdirect, Mchoice, id = ~ID, estimate="amce_diff", feature_labels = feature_names, feature_order = c("outcome.fav", "authorization", "aim", "majority", "composition", "size", "recruitment", "initiative", "issue"), by = ~exsplit)

diff_experience_estimates_choice2 <-bind_rows(
  diff_amce_exp_choice %>% mutate(model = "Familiarity"),
  diff_amce_participated_choice %>% mutate(model = "Participated"),
  diff_amce_expdirect_choice %>% mutate(model = "Positive experience"))

diff_experience_choice2 <- diff_experience_estimates_choice2 %>% select(BY:level, estimate:model)
diff_experience_choice2$model <- factor(diff_experience_choice2$model, levels = c("Familiarity", "Participated", "Positive experience"))

plot(diff_experience_choice2, feature_headers = FALSE, xlab = "Estimated Difference (AMCE)") + 
  ggplot2::facet_wrap(~model, ncol = 3) +
  theme(panel.border = element_rect(linetype = "dotted", colour = "#515151"),
        panel.background = element_blank(), 
        axis.text.y = element_text(colour = "#515151", size = 12),
        axis.text.x = element_text(colour = "#515151", size = 10),
        axis.title = element_text(colour = "#515151", size = 12),
        panel.grid.major = element_line(colour = "grey", linetype = "dotted"),
        strip.background = element_rect(color="gray97", size = 2, fill = "gray97"),
        strip.text.x = element_text(colour = "#515151", size = 14),
        legend.position = "none")+
  geom_point(size=2) +
  scale_color_manual(values = rep("#515151", 9))

#### e) Descriptive information on familiarity and experiences ####
## Reproduce: 
##      - Table 2 (main text)  
##      
## Output: 
##      Table 2: Average support for minipublics conditional on levels of familiarity and direction of experiences 
##      

# check differences in means
describeBy(cj_experience$retro, cj_experience$familiar)
describeBy(cj_experience$retro, cj_experience$participated)
describeBy(cj_expdirect$retro, cj_expdirect$exsplit)

t.test(cj_experience$retro ~ cj_experience$familiar, var.equal = TRUE, alternative = "two.sided") # significant differences (familiar: mean=5.2; unfamiliar: mean=4.76)
cohensD(cj_experience$retro ~ cj_experience$familiar) # Cohens d = 0.32 (small effect)

t.test(cj_experience$retro ~ cj_experience$participated, var.equal = TRUE, alternative = "two.sided") # significant differences (participated: mean=5.53; not participated: mean=4.82)
cohensD(cj_experience$retro ~ cj_experience$participated) # Cohens d = 0.32 (small effect)

t.test(cj_expdirect$retro ~ cj_expdirect$exsplit, var.equal = TRUE, alternative = "two.sided") # significant differences (positive: mean=6.0; negative: mean=4.72)
cohensD(cj_expdirect$retro ~ cj_expdirect$exsplit) # Cohens d = 1.08 (large effect)


##### 4. TRUST: ALL COUNTRIES #####
## Reproduce: 
##      - Figure 4a (main text)
##      - Figure 4b (main text)
## Output: 
##      Figure 4a: Effects of minipublic attributes on support conditional on low political trust 
##      Figure 4b: Effects of minipublic attributes on support conditional on  high trust in other citizens as political decision-makers 
##

#### a) Trust in political institutions ####
# create index for institutional political trust
# relevant variables: trust in parliament and trust in government
#alpha(subset(cj_trust, select = c(trust_parliament, trust_government)), check.keys = TRUE) # alpha .9
cj_trust$trust_politics <- rowMeans(subset(cj_trust, select = c(trust_parliament, trust_government)), na.rm = TRUE)
describe(cj_trust$trust_politics)
cj_trust_politics <- cj_trust %>% filter(trust_politics != "NaN") # only select cases with valid values
describe(cj_trust$trust_politics) #mean 3.84 median 4

# prepare grouping variable for institutional political trust: 
# median split (use dichotomize function)
cj_trust_politics$trustsplit<-dicho(cj_trust_politics$trust_politics, dich.by = "median", as.num = TRUE)
#class(cj_trust_politics$trustsplit)
cj_trust_politics$trustsplit <- factor(cj_trust_politics$trustsplit, labels = c("Low trust", "High trust"))
cj_trust_politics$trustsplit <- relevel(cj_trust_politics$trustsplit, ref = "High trust")
# check whether any of the effects are significantly different
cj_anova(cj_trust_politics, Mchoice, by = ~trustsplit)
summary(cj_trust_politics$trustsplit)

#### b) Trust in politicians ####
# prepare variable for interpersonal political trust
# relevant variable: trust in politicians
describe(cj_trust$trust_politicians) 
trust_politicians_NA <- rowSums(is.na(select(cj_trust, trust_politicians)))
cj_trust_politicians <- filter(cj_trust, trust_politicians_NA == 0) # select cases with valid values
describe(cj_trust_politicians$trust_politicians) #mean 3.34 median 3

# prepare grouping variable for interpersonal political trust:
# median split (use dichotomize function)
cj_trust_politicians$trustsplit<-dicho(cj_trust_politicians$trust_politicians, dich.by = "median", as.num = TRUE)
#class(cj_trust_politicians$trustsplit)
cj_trust_politicians$trustsplit <- factor(cj_trust_politicians$trustsplit, labels = c("Low trust", "High trust"))
cj_trust_politicians$trustsplit <- relevel(cj_trust_politicians$trustsplit, ref = "High trust")
# check whether any of the effects are significantly different
cj_anova(cj_trust_politicians, Mchoice, by = ~trustsplit)
summary(cj_trust_politicians$trustsplit)

#### c) Trust in minipublics ####
# prepare variable for trust in minipublics
# relevant variable: trust in minipublics
describe(cj_trust$trust_dmp) #mean 4.53 median 5

# prepare grouping variable for trust in minipublics:
# median split (use dichotomize function)
cj_trust$dmpsplit<-dicho(cj_trust$trust_dmp, dich.by = "median", as.num = TRUE)
#class(cj_trust$dmpsplit)
cj_trust$dmpsplit <- factor(cj_trust$dmpsplit, labels = c("Low trust", "High trust"))
# check whether any of the effects are significantly different
cj_anova(cj_trust, Mchoice, by = ~dmpsplit)
summary(cj_trust$dmpsplit)

#### d) Trust in other citizens ####
# create index for trust in citizens
# relevant variables: trust in citizens1 and trust in citizens2
#alpha(subset(cj_trust, select = c(trust_citizens1, trust_citizens2)), check.keys = TRUE) # alpha .86
cj_trust$trust_citizens <- rowMeans(subset(cj_trust, select = c(trust_citizens1, trust_citizens2)), na.rm = TRUE)
describe(cj_trust$trust_citizens) #mean 4.44 median 4.5

# prepare grouping variable for interpersonal social trust:
# median split (use dichotomize function)
cj_trust$citizensplit<-dicho(cj_trust$trust_citizens, dich.by = "median", as.num = TRUE)
#class(cj_trust$citizensplit)
cj_trust$citizensplit <- factor(cj_trust$citizensplit, labels = c("Low trust", "High trust"))
# check whether any of the effects are significantly different
cj_anova(cj_trust, Mchoice, by = ~citizensplit)
summary(cj_trust$citizensplit)

#### e) Combined plots for trust (Figure 4a and Figure 4b) ####
# specify and estimate four conjoint models for trust use difference estimates
diff_amce_politics <- cj(cj_trust_politics, Mchoice, id = ~ID, estimate="amce_diff", feature_labels = feature_names, feature_order = c("outcome.fav", "authorization", "aim", "majority", "composition", "size", "recruitment", "initiative", "issue"), by = ~trustsplit)
diff_amce_politicians <- cj(cj_trust_politicians, Mchoice, id = ~ID, estimate="amce_diff", feature_labels = feature_names, feature_order = c("outcome.fav", "authorization", "aim", "majority", "composition", "size", "recruitment", "initiative", "issue"), by = ~trustsplit)
diff_amce_dmp <- cj(cj_trust, Mchoice, id = ~ID, estimate="amce_diff", feature_labels = feature_names, feature_order = c("outcome.fav", "authorization", "aim", "majority", "composition", "size", "recruitment", "initiative", "issue"), by = ~dmpsplit)
diff_amce_citizens <- cj(cj_trust, Mchoice, id = ~ID, estimate="amce_diff", feature_labels = feature_names, feature_order = c("outcome.fav", "authorization", "aim", "majority", "composition", "size", "recruitment", "initiative", "issue"), by = ~citizensplit)

diff_disent_estimates_trust_1 <-bind_rows(
  diff_amce_politics %>% mutate(model = "Political institutions"),
  diff_amce_politicians %>% mutate(model = "Politicians"))

diff_disent_estimates_trust_2 <-bind_rows(
  diff_amce_dmp %>% mutate(model = "Minipublics"),
  diff_amce_citizens %>% mutate(model = "Citizens"))

diff_disent_trust1 <- diff_disent_estimates_trust_1 %>% select(BY:level, estimate:model)
diff_disent_trust1$model <- factor(diff_disent_trust1$model, levels = c("Political institutions", "Politicians"))

diff_disent_trust2 <- diff_disent_estimates_trust_2 %>% select(BY:level, estimate:model)
diff_disent_trust2$model <- factor(diff_disent_trust2$model, levels = c("Minipublics", "Citizens"))

# Figure 4a: Effects of minipublic attributes on support conditional on low political trust 
plot(diff_disent_trust1, feature_headers = FALSE, xlab = "Estimated Difference (AMCE)") + 
  ggplot2::facet_wrap(~model, ncol = 2) +
  theme(panel.border = element_rect(linetype = "dotted", colour = "#515151"),
        panel.background = element_blank(), 
        axis.text.y = element_text(colour = "#515151", size = 12),
        axis.text.x = element_text(colour = "#515151", size = 10),
        axis.title = element_text(colour = "#515151", size = 12),
        panel.grid.major = element_line(colour = "grey", linetype = "dotted"),
        strip.background= element_rect(color="gray97", size = 2, fill = "gray97"),
        strip.text.x = element_text(colour = "#515151", size = 14),
        legend.position = "none")+
  geom_point(size=2) +
  scale_color_manual(values = rep("#515151", 9))

# Figure 4b: Effects of minipublic attributes on support conditional on  high trust in other citizens as political decision-makers
plot(diff_disent_trust2, feature_headers = FALSE, xlab = "Estimated Difference (AMCE)") + 
  ggplot2::facet_wrap(~model, ncol = 2) +
  theme(panel.border = element_rect(linetype = "dotted", colour = "#515151"),
        panel.background = element_blank(), 
        axis.text.y = element_text(colour = "#515151", size = 12),
        axis.text.x = element_text(colour = "#515151", size = 10),
        axis.title = element_text(colour = "#515151", size = 12),
        panel.grid.major = element_line(colour = "grey", linetype = "dotted"),
        strip.background= element_rect(color="gray97", size = 2, fill = "gray97"),
        strip.text.x = element_text(colour = "#515151", size = 14),
        legend.position = "none")+
  geom_point(size=2) +
  scale_color_manual(values = rep("#515151", 9))


#### f) Descriptive information on trust ####
## Reproduce: 
##      - Table 3 (main text)  
##      
## Output: 
##      Table 3: Average support for minipublics conditional on low versus high levels of trust  
##      

# check differences in means
describeBy(cj_trust_politics$retro, cj_trust_politics$trustsplit)
describeBy(cj_trust_politicians$retro, cj_trust_politicians$trustsplit)
describeBy(cj_trust$retro, cj_trust$dmpsplit)
describeBy(cj_trust$retro, cj_trust$citizensplit)

t.test(cj_trust_politics$retro ~ cj_trust_politics$trustsplit, var.equal = TRUE, alternative = "two.sided") # significant differences (high trust: mean=5.06; low trust: mean=4.71)
cohensD(cj_trust_politics$retro ~ cj_trust_politics$trustsplit) # Cohens d = 0.26 (small effect)

t.test(cj_trust_politicians$retro ~ cj_trust_politicians$trustsplit, var.equal = TRUE, alternative = "two.sided") # significant differences (high trust: mean=5; low trust: mean=4.74)
cohensD(cj_trust_politicians$retro ~ cj_trust_politicians$trustsplit) # Cohens d = 0.19 (small effect)

t.test(cj_trust$retro ~ cj_trust$dmpsplit, var.equal = TRUE, alternative = "two.sided") # significant differences (high trust: mean=6.12; low trust: mean=4.51)
cohensD(cj_trust$retro ~ cj_trust$dmpsplit) # Cohens d = 1.36 (large effect)

t.test(cj_trust$retro ~ cj_trust$citizensplit, var.equal = TRUE, alternative = "two.sided") # significant differences (high trust: mean=5.56; low trust: mean=4.31)
cohensD(cj_trust$retro ~ cj_trust$citizensplit) # Cohens d = 1.04 (large effect)

##### 5. LOW POLITICAL BUT HIGH SOCIAL TRUST: COMBINED FOR COUNTRIES #####
## Reproduce: 
##      - Figure 5 (main text)
##      
## Output: 
##      Figure 5: Combined effect for low political trust but high trust in citizens as political decision-makers 
##

#### a) All countries ####
# select cases where political trust is low but social trust is high
cj_trust_NA <- cj_trust %>% select(respondent_id:chosen, outcome.fav:chosen, trust_parliament:trust_politicians, trust_dmp:ID)
# descriptive information on trust variables
describe(cj_trust_NA$trust_government) # mean 3.8 median 4
describe(cj_trust_NA$trust_parliament) # mean 3.89 median 4
describe(cj_trust_NA$trust_politicians) # mean 3.34 median 3
describe(cj_trust_NA$trust_dmp) # mean 4.53 median 5
describe(cj_trust_NA$trust_citizens1) # mean 4.49 median 5
describe(cj_trust_NA$trust_citizens2) # mean 4.4 median 4

# create index for political trust:
# relevant variables: trust in parliament, trust in government and trust in politicians
#alpha(subset(cj_trust_NA, select = c(trust_parliament, trust_government, trust_politicians)), check.keys = TRUE) # alpha .92
cj_trust_NA$poltrust <- rowMeans(subset(cj_trust_NA, select = c(trust_parliament, trust_government, trust_politicians)), na.rm = TRUE)
cj_trust_NA <- cj_trust_NA %>% filter(poltrust != "NaN")

# create index for trust in citizens as decision-makers:
# relevant variables: trust in minipulics, trust in citizens1 and trust in citizens2
#alpha(subset(cj_trust_NA, select = c(trust_dmp, trust_citizens1, trust_citizens2)), check.keys = TRUE) # alpha .84
cj_trust_NA$soctrust <- rowMeans(subset(cj_trust_NA, select = c(trust_dmp, trust_citizens1, trust_citizens2)), na.rm = TRUE)
cj_trust_NA <- cj_trust_NA %>% filter(soctrust != "NaN")

# descriptive information on index political trust and trust in citizens
describe(cj_trust_NA$poltrust) # mean 3.67 median 3.67
describe(cj_trust_NA$soctrust) # mean 4.48 median 4.33

# prepare grouping variable for low political trust but high trust in citizens:
# split scales at midpoint to define cases where political trust is low but social trust is high 
cj_trust_NA$select_trust <- ifelse(cj_trust_NA$poltrust < 4 & cj_trust_NA$soctrust > 4, 1, 0)

# generate table to describe the share of respondents with low political trust but high trust in citizens
f <- table(cj_trust_NA$select_trust)
f_trust <- prop.table(f)
round(f_trust, digits = 3)
fkum <- cumsum(f)
f_trustkum <- cumsum(f_trust)
f_trustkum <- round(f_trustkum, digits = 3)
cbind(f, f_trust, fkum, f_trustkum) # n= 59520; 72.5% in group 0 and 27,5% in group 1 (low political trust and high trust in citizens)

# define trust grouping variable as factor
cj_trust_NA$select_trust <- as.factor(cj_trust_NA$select_trust)
summary(cj_trust_NA$select_trust)

#### b) Finland ####
# prepare conjoint attribute variables:
# as factors +
# labeling
cj_fin$issue<-factor(cj_fin$issue, labels = c("Emissions", "Immigration"))
cj_fin$initiative <-factor(cj_fin$initiative, labels = c("Think Tank/NGO", "Government"))
cj_fin$recruitment <-factor(cj_fin$recruitment, labels = c("Random selection", "Self-selection"))
cj_fin$size <-factor(cj_fin$size, labels = c("Small", "Large"))
cj_fin$composition <-factor(cj_fin$composition, labels = c("Citizens alone", "Mixed groups"))
cj_fin$majority <-factor(cj_fin$majority, labels = c("Narrow majority", "Clear majority"))
cj_fin$aim <-factor(cj_fin$aim, labels = c("Efficiency", "Consideration of all interests"))
cj_fin$outcome.fav <- factor(cj_fin$outcome.fav, labels = c("Preference mismatch","Preference match"))
cj_fin$authorization <-factor(cj_fin$authorization, labels = c("Binding decision",  "Recommendation to public officials", "Recommendation to a public referendum"))
cj_fin$authorization <- relevel(cj_fin$authorization, ref = "Recommendation to public officials")
cj_fin$ID <- factor(cj_fin$respondent_id)

# rename attributes
feature_names <- c(
  'issue' = "Policy Issue",
  'initiative' = "Initiative",
  'recruitment' = "Recruitment",
  'size' = "Group Size",
  'composition' = "Group Composition",
  'majority' = "Degree of Consensus",
  'aim' = "Aim of the forum",
  'authorization' = "Authorization",
  'outcome.fav' = "Outcome Favorability"
)

# specifying conjoint models
Mchoice <- chosen ~ issue + initiative + recruitment + size + composition + majority + aim + authorization + outcome.fav # conjoint with all attributes, outcome favorability, choice outcome
# select relevant trust variables
cj_fin_NA <- cj_fin %>% select(respondent_id:chosen, outcome.fav:chosen, trust_parliament:trust_politicians, trust_dmp:ID)

# create index for political trust:
# relevant variables: trust in parliament, trust in government and trust in politicians
#alpha(subset(cj_fin_NA, select = c(trust_parliament, trust_government, trust_politicians)), check.keys = TRUE) # alpha .91
cj_fin_NA$poltrust <- rowMeans(subset(cj_fin_NA, select = c(trust_parliament, trust_government, trust_politicians)), na.rm = TRUE)
cj_fin_NA <- cj_fin_NA %>% filter(poltrust != "NaN")

# create index for trust in citizens:
# relevant variables: trust in minipublics, trust in citizens1 and trust in citizens2
#alpha(subset(cj_fin_NA, select = c(trust_dmp, trust_citizens1, trust_citizens2)), check.keys = TRUE) # alpha .83
cj_fin_NA$soctrust <- rowMeans(subset(cj_fin_NA, select = c(trust_dmp, trust_citizens1, trust_citizens2)), na.rm = TRUE)
cj_fin_NA <- cj_fin_NA %>% filter(soctrust != "NaN")

# descriptive information on index political trust and social trust
describe(cj_fin_NA$poltrust) # mean 4.01 median 4
describe(cj_fin_NA$soctrust) # mean 4.27 median 4.33

# prepare grouping variable for low political but high social trust:
# split scales at midpoint to define cases where political trust is low but social trust is high 
cj_fin_NA$select_trust <- ifelse(cj_fin_NA$poltrust < 4 & cj_fin_NA$soctrust > 4, 1, 0)

f2 <- table(cj_fin_NA$select_trust)
f2_trust <- prop.table(f2)
round(f2_trust, digits = 3)
f2kum <- cumsum(f2)
f2_trustkum <- cumsum(f2_trust)
f2_trustkum <- round(f2_trustkum, digits = 3)
cbind(f2, f2_trust, f2kum, f2_trustkum) # n= 19690; 80.4% in group 0 and 19.6% in group 1 (low political trust and high social trust)

# define trust variable as factor
cj_fin_NA$select_trust <- as.factor(cj_fin_NA$select_trust)
summary(cj_fin_NA$select_trust)

#### c) Ireland ####
# prepare conjoint attribute variables:
# as factors +
# labeling
cj_ire$issue<-factor(cj_ire$issue, labels = c("Emissions", "Immigration"))
cj_ire$initiative <-factor(cj_ire$initiative, labels = c("Think Tank/NGO", "Government"))
cj_ire$recruitment <-factor(cj_ire$recruitment, labels = c("Random selection", "Self-selection"))
cj_ire$size <-factor(cj_ire$size, labels = c("Small", "Large"))
cj_ire$composition <-factor(cj_ire$composition, labels = c("Citizens alone", "Mixed groups"))
cj_ire$majority <-factor(cj_ire$majority, labels = c("Narrow majority", "Clear majority"))
cj_ire$aim <-factor(cj_ire$aim, labels = c("Efficiency", "Consideration of all interests"))
cj_ire$outcome.fav <- factor(cj_ire$outcome.fav, labels = c("Preference mismatch","Preference match"))
cj_ire$authorization <-factor(cj_ire$authorization, labels = c("Binding decision",  "Recommendation to public officials", "Recommendation to a public referendum"))
cj_ire$authorization <- relevel(cj_ire$authorization, ref = "Recommendation to public officials")
cj_ire$ID <- factor(cj_ire$respondent_id)

# rename attributes
feature_names <- c(
  'issue' = "Policy Issue",
  'initiative' = "Initiative",
  'recruitment' = "Recruitment",
  'size' = "Group Size",
  'composition' = "Group Composition",
  'majority' = "Degree of Consensus",
  'aim' = "Aim of the forum",
  'authorization' = "Authorization",
  'outcome.fav' = "Outcome Favorability"
)

# specifying conjoint models
Mchoice <- chosen ~ issue + initiative + recruitment + size + composition + majority + aim + authorization + outcome.fav # conjoint with all attributes, outcome favorability, choice outcome
# select relevant trust variables
cj_ire_NA <- cj_ire %>% select(respondent_id:chosen, outcome.fav:chosen, trust_parliament:trust_politicians, trust_dmp:ID)

# create index for political trust:
# relevant variables: trust in parliament, trust in government and trust in politicians
#alpha(subset(cj_ire_NA, select = c(trust_parliament, trust_government, trust_politicians)), check.keys = TRUE) # alpha .91
cj_ire_NA$poltrust <- rowMeans(subset(cj_ire_NA, select = c(trust_parliament, trust_government, trust_politicians)), na.rm = TRUE)
cj_ire_NA <- cj_ire_NA %>% filter(poltrust != "NaN")

# create index for trust in citizens:
# relevant variables: trust in minipublics, trust in citizens1 and trust in citizens2
#alpha(subset(cj_ire_NA, select = c(trust_dmp, trust_citizens1, trust_citizens2)), check.keys = TRUE) # alpha .82
cj_ire_NA$soctrust <- rowMeans(subset(cj_ire_NA, select = c(trust_dmp, trust_citizens1, trust_citizens2)), na.rm = TRUE)
cj_ire_NA <- cj_ire_NA %>% filter(soctrust != "NaN")

# descriptive information on index political trust an social trust
describe(cj_ire_NA$poltrust) # mean 3.81 median 4
describe(cj_ire_NA$soctrust) # mean 4.69 median 4.67

# prepare grouping variable for low political but high social trust:
# split scales at midpoint to define cases where political trust is low but social trust is high 
cj_ire_NA$select_trust <- ifelse(cj_ire_NA$poltrust < 4 & cj_ire_NA$soctrust > 4, 1, 0)

f3 <- table(cj_ire_NA$select_trust)
f3_trust <- prop.table(f3)
round(f3_trust, digits = 3)
f3kum <- cumsum(f3)
f3_trustkum <- cumsum(f3_trust)
f3_trustkum <- round(f3_trustkum, digits = 3)
cbind(f3, f3_trust, f3kum, f3_trustkum) # n= 19880; 70.6% in group 0 and 29.4% in group 1 (low political trust and high social trust)

# define trust variable as factor
cj_ire_NA$select_trust <- as.factor(cj_ire_NA$select_trust)
summary(cj_ire_NA$select_trust)

#### d) USA ####
# prepare conjoint attribute variables:
# as factors +
# labeling
cj_usa$issue<-factor(cj_usa$issue, labels = c("Emissions", "Immigration"))
cj_usa$initiative <-factor(cj_usa$initiative, labels = c("Think Tank/NGO", "Government"))
cj_usa$recruitment <-factor(cj_usa$recruitment, labels = c("Random selection", "Self-selection"))
cj_usa$size <-factor(cj_usa$size, labels = c("Small", "Large"))
cj_usa$composition <-factor(cj_usa$composition, labels = c("Citizens alone", "Mixed groups"))
cj_usa$majority <-factor(cj_usa$majority, labels = c("Narrow majority", "Clear majority"))
cj_usa$aim <-factor(cj_usa$aim, labels = c("Efficiency", "Consideration of all interests"))
cj_usa$outcome.fav <- factor(cj_usa$outcome.fav, labels = c("Preference mismatch","Preference match"))
cj_usa$authorization <-factor(cj_usa$authorization, labels = c("Binding decision",  "Recommendation to public officials", "Recommendation to a public referendum"))
cj_usa$authorization <- relevel(cj_usa$authorization, ref = "Recommendation to public officials")
cj_usa$ID <- factor(cj_usa$respondent_id)

# rename attributes
feature_names <- c(
  'issue' = "Policy Issue",
  'initiative' = "Initiative",
  'recruitment' = "Recruitment",
  'size' = "Group Size",
  'composition' = "Group Composition",
  'majority' = "Degree of Consensus",
  'aim' = "Aim of the forum",
  'authorization' = "Authorization",
  'outcome.fav' = "Outcome Favorability"
)

# specifying conjoint models
Mchoice <- chosen ~ issue + initiative + recruitment + size + composition + majority + aim + authorization + outcome.fav # conjoint with all attributes, outcome favorability, choice outcome
# select relevant trust variables
cj_usa_NA <- cj_usa %>% select(respondent_id:chosen, outcome.fav:chosen, trust_parliament:trust_politicians, trust_dmp:ID)

# create index for political trust:
# relevant variables: trust in parliament, trust in government and trust in politicians
# alpha(subset(cj_usa_NA, select = c(trust_parliament, trust_government, trust_politicians)), check.keys = TRUE) # alpha .93
cj_usa_NA$poltrust <- rowMeans(subset(cj_usa_NA, select = c(trust_parliament, trust_government, trust_politicians)), na.rm = TRUE)
cj_usa_NA <- cj_usa_NA %>% filter(poltrust != "NaN")

# create index for trust in citizens:
# relevant variables: trust in minipublics, trust in citizens1 and trust in citizens2
# alpha(subset(cj_usa_NA, select = c(trust_dmp, trust_citizens1, trust_citizens2)), check.keys = TRUE) # alpha .87
cj_usa_NA$soctrust <- rowMeans(subset(cj_usa_NA, select = c(trust_dmp, trust_citizens1, trust_citizens2)), na.rm = TRUE)
cj_usa_NA <- cj_usa_NA %>% filter(soctrust != "NaN")

# descriptive information on index political trust and social trust
describe(cj_usa_NA$poltrust) # mean 3.2 median 3
describe(cj_usa_NA$soctrust) # mean 4.48 median 4.33

# prepare grouping variable for low political but high social trust:
# split scales at midpoint to define cases where political trust is low but social trust is high 
cj_usa_NA$select_trust <- ifelse(cj_usa_NA$poltrust < 4 & cj_usa_NA$soctrust > 4, 1, 0)

f4 <- table(cj_usa_NA$select_trust)
f4_trust <- prop.table(f4)
round(f4_trust, digits = 3)
f4kum <- cumsum(f4)
f4_trustkum <- cumsum(f4_trust)
f4_trustkum <- round(f4_trustkum, digits = 3)
cbind(f4, f4_trust, f4kum, f4_trustkum) # n= 19950; 66.6% in group 0 and 33.4% in group 1 (low political trust and high social trust)

# define trust variable as factor
cj_usa_NA$select_trust <- as.factor(cj_usa_NA$select_trust)
summary(cj_usa_NA$select_trust)

#### e) Combined plots for low political but high social trust  (Figure 5) ####
# specify and estimate four conjoint models for trust (all countries, FIN, IRE, USA) separately use difference estimates
diff_amce_select_all <- cj(cj_trust_NA, Mchoice, id = ~ID, estimate="amce_diff", feature_labels = feature_names, feature_order = c("outcome.fav", "authorization", "aim", "majority", "composition", "size", "recruitment", "initiative", "issue"), by = ~select_trust)
diff_amce_select_fin <- cj(cj_fin_NA, Mchoice, id = ~ID, estimate="amce_diff", feature_labels = feature_names, feature_order = c("outcome.fav", "authorization", "aim", "majority", "composition", "size", "recruitment", "initiative", "issue"), by = ~select_trust)
diff_amce_select_ire<- cj(cj_ire_NA, Mchoice, id = ~ID, estimate="amce_diff", feature_labels = feature_names, feature_order = c("outcome.fav", "authorization", "aim", "majority", "composition", "size", "recruitment", "initiative", "issue"), by = ~select_trust)
diff_amce_select_usa <- cj(cj_usa_NA, Mchoice, id = ~ID, estimate="amce_diff", feature_labels = feature_names, feature_order = c("outcome.fav", "authorization", "aim", "majority", "composition", "size", "recruitment", "initiative", "issue"), by = ~select_trust)

diff_disent_estimates_select <-bind_rows(
  diff_amce_select_all %>% mutate(model = "All countries"),
  diff_amce_select_fin %>% mutate(model = "Finland"),
  diff_amce_select_ire %>% mutate(model = "Ireland"),
  diff_amce_select_usa %>% mutate(model = "USA"))

diff_disent_select <- diff_disent_estimates_select %>% select(BY:level, estimate:model)
diff_disent_select$model <- factor(diff_disent_select$model, levels = c("All countries", "Finland", "Ireland", "USA"))


plot(diff_disent_select, feature_headers = FALSE, xlab = "Estimated Difference (AMCE)") + 
  ggplot2::facet_wrap(~model, ncol = 2) +
  theme(panel.border = element_rect(linetype = "dotted", colour = "#515151"),
        panel.background = element_blank(), 
        axis.text.y = element_text(colour = "#515151", size = 12),
        axis.text.x = element_text(colour = "#515151", size = 10),
        axis.title = element_text(colour = "#515151", size = 12),
        panel.grid.major = element_line(colour = "grey", linetype = "dotted"),
        strip.background= element_rect(color="gray97", size = 2, fill = "gray97"),
        strip.text.x = element_text(colour = "#515151", size = 14),
        legend.position = "none")+
  geom_point(size=2) +
  scale_color_manual(values = rep("#515151", 9))
